package com.jing.android.notes.ui.widget;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 带箭头的圆角矩形背景图
 * @author JingTuo
 */
public class ArrowDrawable extends Drawable {

    private Paint mPaint;

    private int solidColor;

    private float radius;

    private float strokeWidth;

    private int strokeColor;

    private Path mStrokePath;

    private Path mSolidPath;

    private RectF mStrokeBounds;

    private RectF mSolidBounds;

    private Rect mPadding;

    private int arrowWidth;

    private int arrowHeight;

    public ArrowDrawable() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(solidColor);

        mStrokePath = new Path();
        mSolidPath = new Path();
        mStrokeBounds = new RectF();
        mSolidBounds = new RectF();
        mPadding = new Rect();
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        float strokeRadius = strokeWidth / 2;
        float strokeBottom = bounds.bottom - arrowHeight + strokeRadius;
        mStrokeBounds.set(bounds.left + strokeRadius, bounds.top + strokeRadius,
                bounds.right - strokeRadius, strokeBottom);
        mStrokePath.addRoundRect(mStrokeBounds, radius, radius, Path.Direction.CW);

        Path path = new Path();
        path.moveTo(bounds.width() - arrowWidth / 2f, strokeBottom);
        path.lineTo(bounds.width() / 2f, bounds.bottom - strokeRadius);
        path.lineTo(bounds.width() / 2f + arrowWidth / 2f, bounds.bottom - strokeRadius);
        mStrokePath.addPath(path);

        mSolidBounds.set(bounds.left + strokeWidth, bounds.top + strokeWidth,
                bounds.right - strokeWidth, bounds.bottom - strokeWidth);
        mSolidPath.addRoundRect(mSolidBounds, radius, radius, Path.Direction.CW);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        //绘制solid
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(solidColor);
        canvas.drawPath(mSolidPath, mPaint);
        //绘制stroke
        if (strokeWidth > 0) {
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(strokeColor);
            mPaint.setStrokeWidth(strokeWidth);
            canvas.drawPath(mStrokePath, mPaint);
        }
        //绘制下方箭头

    }

    @Override
    public void setAlpha(int alpha) {

    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {

    }

    @Override
    public int getOpacity() {
        return PixelFormat.OPAQUE;
    }

    @Override
    public boolean getPadding(@NonNull Rect padding) {
        if (mPadding != null) {
            padding.set(mPadding.left, mPadding.top, mPadding.right, mPadding.bottom + arrowHeight);
            return true;
        }
        return super.getPadding(padding);
    }

    public void setSolidColor(int solidColor) {
        this.solidColor = solidColor;
    }

    public void setRadius(float radius) {
        this.radius = radius;
    }

    public void setStrokeWidth(float strokeWidth) {
        this.strokeWidth = strokeWidth;
    }

    public void setStrokeColor(int strokeColor) {
        this.strokeColor = strokeColor;
    }

    public void setPadding(int left, int top, int right, int bottom) {
        if ((left | top | right | bottom) == 0) {
            mPadding = null;
        } else {
            if (mPadding == null) {
                mPadding = new Rect();
            }
            mPadding.set(left, top, right, bottom);
        }
        invalidateSelf();
    }

    public void setArrowHeight(int arrowHeight) {
        this.arrowHeight = arrowHeight;
    }

    public void setArrowWidth(int arrowWidth) {
        this.arrowWidth = arrowWidth;
    }
}
